//
//  ZYVistorView.swift
//  weibo007
//
//  Created by 陈宗毅 on 15/12/15.
//  Copyright © 2015年 czy. All rights reserved.
//

import UIKit


protocol ZYVistorViewDelegate:NSObjectProtocol
{
    //协议里面默认是必须实现的
    //注册按钮被点击
    func visitorViewRegisterClick()
    
    //登录按钮被点击
    func visitorViewLoginClick()
}


class ZYVistorView: UIView {
    // 代理, 有可能有,有可能没有
    // 默认是strong
    weak var delegate: ZYVistorViewDelegate?

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
     
        prepareUI()
        //背景颜色
        self.backgroundColor = UIColor(white: 237.0/255, alpha: 1)
    }
    
    
    func startRotationAnition()
    {
        //核心动画，旋转
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置参数
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //动画完成时不移除
        anim.removedOnCompletion = false
        //给图层添加核心动画
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    /// 暂停旋转
    func pauseAnimation(){
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    /// 恢复动画
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    //访客视图提供一个方法让别人来设置内容
    //只有除首页外的3各控制器来设置内容
    func setupVisitorInfo(imageName:String,message:String){
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        //隐藏小房子
        homeView.hidden = true
        //遮盖放到最下面
        sendSubviewToBack(converView)
//        converView.hidden = true
    }
    
    //准备UI
    private func prepareUI()
    {
        addSubview(iconView)
        addSubview(converView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        converView.translatesAutoresizingMaskIntoConstraints = false
        
        //将约束对象添加到父控件self上面去
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //centerY
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:-40))
        
        // 房子参照转轮
        // CenterX和转轮的CenterX重合
        // 约束统一添加到父控件上面
        
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 消息label CenterX和转轮CenterX重合
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 消息label的顶部,距离转轮的底部16
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 注册按钮
        // 宽100
        // 不参照任何对象: toItem 填nil attribute: 必须填: NSLayoutAttribute.NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度35
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        // 注册按钮顶部距离消息label 底部16
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 消息label宽度约束
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // 注册按钮左边和消息label左边重合
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 登陆按钮
        // 宽100
        // 不参照任何对象: toItem 填nil attribute: 必须填: NSLayoutAttribute.NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度35
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        // 登陆按钮顶部距离消息label 底部16
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 登陆按钮右边和消息label右边重合
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //遮罩的底部跟按钮的底部 重合
        self.addConstraint(NSLayoutConstraint(item: converView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //遮罩的顶部跟self的顶部 重合
        self.addConstraint(NSLayoutConstraint(item: converView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //遮罩的顶部跟self的左边 重合
        self.addConstraint(NSLayoutConstraint(item: converView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //遮罩的顶部跟self的右边 重合
        self.addConstraint(NSLayoutConstraint(item: converView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        
    }

    //MARK: - 懒加载  转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //小房子
    private lazy var homeView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //遮罩
    private lazy var converView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //消息
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        
        //设置文字
        label.text = "关注一些人,看看这里有什么惊喜!"
        label.textColor = UIColor.darkGrayColor()
        //显示多行
        label.numberOfLines = 0
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton:UIButton = {
      let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //设置按钮的文字
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button .addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        //设置文字颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return button
    }()
    
    //登录按钮
    private lazy var loginButton:UIButton = {
    
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button .addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //文字颜色
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        return button
    }()
    


    
    func registerBtnClick(){
//        print("registerBtnClick");
        // 调用代理通知控制器按钮被点击
        // delegate有值就调用visitorViewRegisterClick
        // delegate没有值就不执行visitorViewRegisterClick
        delegate?.visitorViewRegisterClick()
    }
    
    func loginBtnClick(){
//        print("loginBtnClick")
        delegate?.visitorViewLoginClick()
    }
 
}
